<template>
  <d2-container>
    <div class="mod-role">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList(true)" size="small">
        <!-- 移除嵌套的 el-form-item -->
        <el-form-item label="ICCID">
          <el-input v-model="dataForm.iccid" placeholder="请输入 ICCID"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList(true)">查询</el-button>
          <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
          <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">导入</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;">
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column
          prop="accessNumber"
          header-align="center"
          align="center"
          label="接入号">
        </el-table-column>
        <el-table-column
          prop="iccid"
          header-align="center"
          align="center"
          label="ICCID">
        </el-table-column>
        <el-table-column
          prop="operator"
          header-align="center"
          align="center"
          label="运营商">
        </el-table-column>
        <el-table-column
          prop="customerName"
          header-align="center"
          align="center"
          label="客户名称">
        </el-table-column>
        <el-table-column
          prop="expiryDate"
          header-align="center"
          align="center"
          label="到期时间">
        </el-table-column>
        <el-table-column
          prop="prepaid"
          header-align="center"
          align="center"
          label="是否预充值">
        </el-table-column>
        <el-table-column prop="cardStatus" label="卡状态">
          <template #default="{ row }">
            <span :style="{ color: row.cardStatus === '欠费' ? 'red' : 'green' }">{{ row.cardStatus }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="lifeCycle"
          header-align="center"
          align="center"
          label="生命周期">
        </el-table-column>
        <el-table-column
          prop="balance"
          header-align="center"
          align="center"
          label="余额(MB)">
        </el-table-column>
        <el-table-column
          prop="monthlyUsage"
          header-align="center"
          align="center"
          label="当月用量(MB)">
        </el-table-column>
        <el-table-column
          prop="packageTotal"
          header-align="center"
          align="center"
          label="套餐总量(MB)">
        </el-table-column>
        <el-table-column
          prop="packageName"
          header-align="center"
          align="center"
          label="套餐">
        </el-table-column>
        <el-table-column
          prop="packageType"
          header-align="center"
          align="center"
          label="套餐类型">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="120"> <!-- 修改宽度为 120 -->
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.roleId)">修改</el-button>
            <el-button type="text" size="small" @click="deleteHandle(scope.row.roleId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="mt-4"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </d2-container>
</template>

<script>
  import AddOrUpdate from './role-add-or-update'
  export default {
    name: 'sys_simcard',
    data() {
      return {
        dataForm: {},
        dataList: [
          {
            accessNumber: '13800138000',
            iccid: '89860012345678901234',
            operator: '中国移动',
            customerName: '张三',
            expiryDate: '2024-12-31',
            prepaid: '是',
            cardStatus: '正常',
            lifeCycle: '激活',
            balance: 1024,
            monthlyUsage: 512,
            packageTotal: 2048,
            packageName: '5G流量套餐',
            packageType: '包月'
          },
          {
            accessNumber: '13900139001',
            iccid: '89860012345678901231',
            operator: '中国联通',
            customerName: '李四',
            expiryDate: '2025-01-31',
            prepaid: '否',
            cardStatus: '欠费',
            lifeCycle: '冻结',
            balance: 256,
            monthlyUsage: 768,
            packageTotal: 1024,
            packageName: '3G流量套餐',
            packageType: '包季'
          },
          {
            accessNumber: '15000150002',
            iccid: '89860012345678901232',
            operator: '中国电信',
            customerName: '王五',
            expiryDate: '2025-02-28',
            prepaid: '是',
            cardStatus: '正常',
            lifeCycle: '激活',
            balance: 2048,
            monthlyUsage: 1024,
            packageTotal: 4096,
            packageName: '10G流量套餐',
            packageType: '包年'
          },
          {
            accessNumber: '15100151003',
            iccid: '89860012345678901233',
            operator: '中国移动',
            customerName: '赵六',
            expiryDate: '2025-03-31',
            prepaid: '否',
            cardStatus: '停机',
            lifeCycle: '注销',
            balance: 0,
            monthlyUsage: 1536,
            packageTotal: 1536,
            packageName: '临时套餐',
            packageType: '一次性'
          },
          {
            accessNumber: '15200152004',
            iccid: '89860012345678901234',
            operator: '中国联通',
            customerName: '孙七',
            expiryDate: '2025-04-30',
            prepaid: '是',
            cardStatus: '正常',
            lifeCycle: '激活',
            balance: 512,
            monthlyUsage: 256,
            packageTotal: 1024,
            packageName: '2G流量套餐',
            packageType: '包月'
          },
          {
            accessNumber: '15300153005',
            iccid: '89860012345678901235',
            operator: '中国电信',
            customerName: '周八',
            expiryDate: '2025-05-31',
            prepaid: '否',
            cardStatus: '欠费',
            lifeCycle: '冻结',
            balance: 128,
            monthlyUsage: 384,
            packageTotal: 512,
            packageName: '1G流量套餐',
            packageType: '包季'
          },
          {
            accessNumber: '15400154006',
            iccid: '89860012345678901236',
            operator: '中国移动',
            customerName: '吴九',
            expiryDate: '2025-06-30',
            prepaid: '是',
            cardStatus: '正常',
            lifeCycle: '激活',
            balance: 3072,
            monthlyUsage: 1536,
            packageTotal: 4096,
            packageName: '15G流量套餐',
            packageType: '包年'
          },
          {
            accessNumber: '15500155007',
            iccid: '89860012345678901237',
            operator: '中国联通',
            customerName: '郑十',
            expiryDate: '2025-07-31',
            prepaid: '否',
            cardStatus: '停机',
            lifeCycle: '注销',
            balance: 0,
            monthlyUsage: 2048,
            packageTotal: 2048,
            packageName: '临时套餐',
            packageType: '一次性'
          },
          {
            accessNumber: '15600156008',
            iccid: '89860012345678901238',
            operator: '中国电信',
            customerName: '陈十一',
            expiryDate: '2025-08-31',
            prepaid: '是',
            cardStatus: '正常',
            lifeCycle: '激活',
            balance: 1024,
            monthlyUsage: 512,
            packageTotal: 2048,
            packageName: '5G流量套餐',
            packageType: '包月'
          },
          {
            accessNumber: '15700157009',
            iccid: '89860012345678901239',
            operator: '中国移动',
            customerName: '杨十二',
            expiryDate: '2025-09-30',
            prepaid: '否',
            cardStatus: '欠费',
            lifeCycle: '冻结',
            balance: 256,
            monthlyUsage: 768,
            packageTotal: 1024,
            packageName: '3G流量套餐',
            packageType: '包季'
          }
        ],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 120,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated() {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList(type) {
        if (type) {
          this.pageIndex = 1
        }
        this.dataListLoading = true
        // 替换为实际的 API 接口
        this.$api.sys_simcard_list({
          page: this.pageIndex,
          limit: this.pageSize,
          customerId: this.$util.cookies.get('customer_id')
        }).then((data) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle(id) {
        const ids = id ? [id] : this.dataListSelections.map(item => item.id)
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 替换为实际的删除 API 接口
          this.$api.sys_simcard_delete(ids).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      },
      handleEdit(row) {
        // 这里添加修改逻辑
        console.log('修改操作', row);
      },
      handleDelete(row) {
        // 这里添加删除逻辑
        console.log('删除操作', row);
      }
    }
  }
</script>
